import http from '../../util/http';
import * as api from '../../util/api';

const state ={
    GoodsDada:[],
    GoodsDada1:[],
    currentData:0,
    num:0,
    id:0
};

const actions={
    Data({commit},num){  //查询商品列表
        if(num===undefined){
            num=1;
        }
        num=num++
        // console.log(num)
        return http({
            url:api.Data,
            method:'post',
            data:{currentPage:num,perSize:5}
        }).then(res=>{
            commit('getGoods',res.data.data.goodsList)
            commit('getCount',res.data.data.totalCount)
            // console.log(res.data.data.goodsList)
            return res.data.data //将数据返回给页面，处理异步
        }).catch(err=>{
            console.log(err)
        })
    },
    Data1({commit},num){  //查询商品列表 图片
        if(num===undefined){
            num=1;
        }
        num=num++
        // console.log(num)
        return http({
            url:api.Data1,
            method:'post',
            data:{currentPage:num,perSize:8}
        }).then(res=>{
            commit('getGoods1',res.data.data.goodsList)
            commit('getCount',res.data.data.totalCount)
            // console.log(res.data.data.goodsList)
            return res.data.data //将数据返回给页面，处理异步
        }).catch(err=>{
            console.log(err)
        })
    },
    queryGoodsByKeyword({commit},command){          //商品排序
        if(command===undefined){
            command=0;
        }
        command=command++
       // console.log(command)
        return http({
            url:api.queryGoodsByKeyword,
            method:'post',
            data:{
                currentPage:1,
                perSize:5,
                selectValue:command,
                inputValue:0,
            }
        }).then(res=>{
            commit('selectGoods',res.data.data.goodsList)
            // console.log(res.data.data.goodsList)
            return res.data.data //将数据返回给页面，处理异步
        })
    },
    queryGoodsByKeyword1({commit},command){          //商品图片排序
        if(command===undefined){
            command=0;
        }
        command=command++
        // console.log(command)
        return http({
            url:api.queryGoodsByKeyword,
            method:'post',
            data:{
                currentPage:1,
                perSize:8,
                selectValue:command,
                inputValue:0,
            }
        }).then(res=>{
            commit('selectGoods1',res.data.data.goodsList)
            // console.log(res.data.data.goodsList)
            return res.data.data //将数据返回给页面，处理异步
        })
    },
    changeNum({commit},obj){
        commit('changenum',obj)
    },
    getGoodId({commit},obj){
        commit('getId',obj)
    },
    queryGoodsName({commit},name){   //收索商品
        // console.log(name)
        return http({
            url:api.queryGoodsName,
            method:'post',
            data:{
                currentPage:1,
                perSize:5,
                goodsName:name,
            }
        }).then(res=>{
            commit('queryGoods',res.data.data.goodsList)
            commit('getqueryGoodsCount',res.data.data.totalCount)
            console.log(res.data.data.goodsList)
            return res.data.data //将数据返回给页面，处理异步
        })
    }
}


const mutations={
    getGoods(state,obj){
        state.GoodsDada = obj
    },
    getGoods1(state,obj){
        state.GoodsDada1 = obj
    },
    getCount(state,obj){    //分页
        state.currentData = obj
    },
    getqueryGoodsCount(state,obj){    //搜索后分页
        state.currentData = obj
    },
    selectGoods(state,obj){      //排序
        state.GoodsDada = obj
    },
    selectGoods1(state,obj){      //排序
        state.GoodsDada1 = obj
    },
    queryGoods(state,obj){      //搜索
        state.GoodsDada = obj
    },
    changenum(state,obj){
        state.num=obj.num
    },
    getId(state,obj){
        state.id=obj.id
    }
}

export default {
    state,
    actions,
    mutations
}